package com.zbkj.service.service.impl.splittingAccount;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.constants.OrderConstants;
import com.zbkj.common.model.splittingAccount.EbCommissionTransaction;
import com.zbkj.common.model.splittingAccount.EbMerchantAccount;
import com.zbkj.common.model.splittingAccount.EbSplitAccount;
import com.zbkj.common.model.splittingAccount.EbWithdrawalApplication;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.splittingAccount.SplittingAccountRequest;
import com.zbkj.common.response.EbGroupBuyShopResponse;
import com.zbkj.common.utils.CrmebUtil;
import com.zbkj.service.dao.splittingAccount.EbCommissionTransactionDao;
import com.zbkj.service.dao.splittingAccount.EbMerchantAccountDao;
import com.zbkj.service.dao.splittingAccount.EbSplitAccountDao;
import com.zbkj.service.dao.splittingAccount.EbWithdrawalApplicationDao;
import com.zbkj.service.service.splittingAccount.SplittingAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 分账信息
 */
@Slf4j
@Service
public class ISplittingAccountServiceImpl implements SplittingAccountService {
    @Resource
    EbCommissionTransactionDao ebCommissionTransactionDao;
    @Resource
    EbMerchantAccountDao ebMerchantAccountDao;
    @Resource
    EbSplitAccountDao ebSplitAccountDao;
    @Resource
    EbWithdrawalApplicationDao ebWithdrawalApplicationDao;


    /**
     * 商户账户分页查询
     *
     * @param request 分页请求参数
     * @return 分页结果
     */
    @Override
    public PageInfo<EbMerchantAccount> getMerchantAccountPageList(SplittingAccountRequest request) {
        Page<EbGroupBuyShopResponse> page = PageHelper.startPage(request.getPage(), request.getLimit());

        LambdaQueryWrapper<EbMerchantAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(request.getMerId() != null, EbMerchantAccount::getMerId, request.getMerId())
                .like(request.getMerName() != null, EbMerchantAccount::getMerName, request.getMerName())
                .orderByDesc(EbMerchantAccount::getId);
        List<EbMerchantAccount> responseList = ebMerchantAccountDao.selectList(queryWrapper);
        return CommonPage.copyPageInfo(page, responseList);
    }

    /**
     * 商户佣金交易记录分页查询
     *
     * @param request 分页请求参数
     * @return 分页结果
     */
    @Override
    public PageInfo<EbCommissionTransaction> getCommissionTransactionPageList(SplittingAccountRequest request) {


        Page<EbCommissionTransaction> page = PageHelper.startPage(request.getPage(), request.getLimit());
        LambdaQueryWrapper<EbCommissionTransaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(request.getAccountId() != null, EbCommissionTransaction::getAccountId, request.getAccountId())
                .between(request.getStartDate() != null && request.getEndDate() != null,
                        EbCommissionTransaction::getCreateTime, request.getStartDate(), request.getEndDate())
                .orderByDesc(EbCommissionTransaction::getId);

        List<EbCommissionTransaction> responseList = ebCommissionTransactionDao.selectList(queryWrapper);
        return CommonPage.copyPageInfo(page, responseList);
    }

    /**
     * 商户分账记录分页查询
     *
     * @param request 分页请求参数
     * @return 分页结果
     */
    @Override
    public PageInfo<EbSplitAccount> getSplitAccountPageList(SplittingAccountRequest request) {


        Page<EbSplitAccount> page = PageHelper.startPage(request.getPage(), request.getLimit());
        LambdaQueryWrapper<EbSplitAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(request.getMerId() != null, EbSplitAccount::getMerId, request.getMerId())
                .eq(EbSplitAccount::getStatus, true)
                .eq(request.getOrderType() != null, EbSplitAccount::getOrderType, request.getOrderType())
                .between(request.getStartDate() != null && request.getEndDate() != null,
                        EbSplitAccount::getCreateTime, request.getStartDate(), request.getEndDate())
                .orderByDesc(EbSplitAccount::getId);

        List<EbSplitAccount> responseList = ebSplitAccountDao.selectList(queryWrapper);
        return CommonPage.copyPageInfo(page, responseList);
    }

    /**
     * 商户提现申请分页查询
     *
     * @param request 分页请求参数
     * @return 分页结果
     */
    @Override
    public PageInfo<EbWithdrawalApplication> getWithdrawalApplicationPageList(SplittingAccountRequest request) {


        Page<EbWithdrawalApplication> page = PageHelper.startPage(request.getPage(), request.getLimit());
        LambdaQueryWrapper<EbWithdrawalApplication> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(request.getMerId() != null, EbWithdrawalApplication::getMerId, request.getMerId())
                .between(request.getStartDate() != null && request.getEndDate() != null,
                        EbWithdrawalApplication::getCreateTime, request.getStartDate(), request.getEndDate())
                .orderByDesc(EbWithdrawalApplication::getId);

        List<EbWithdrawalApplication> responseList = ebWithdrawalApplicationDao.selectList(queryWrapper);
        return CommonPage.copyPageInfo(page, responseList);
    }

    /**
     * 商户提现申请审核
     */
    @Override
    public EbWithdrawalApplication reviewWithdrawalApplication(EbWithdrawalApplication ebWithdrawalApplication) {
        EbWithdrawalApplication withdrawalApplication = ebWithdrawalApplicationDao.selectById(ebWithdrawalApplication.getId());
        if (withdrawalApplication != null) {
            withdrawalApplication.setAuditStatus(ebWithdrawalApplication.getAuditStatus());
            withdrawalApplication.setAuditRemark(ebWithdrawalApplication.getAuditRemark());
            withdrawalApplication.setUpdateTime(new Date());
            ebWithdrawalApplicationDao.updateById(withdrawalApplication);
        }
        return withdrawalApplication;
    }

    /**
     * 添加分账信息
     *
     * @param ebSplitAccount
     */
    @Override
    public EbSplitAccount addSplitAccount(EbSplitAccount ebSplitAccount) {
        ebSplitAccount.setCreateTime(new Date());
        ebSplitAccount.setUpdateTime(new Date());
        ebSplitAccount.setStatus(false);
        ebSplitAccountDao.insert(ebSplitAccount);

        return ebSplitAccount;

    }

    //获取分账信息
    @Override
    public List<EbSplitAccount> getSplitAccount(String orderNo) {

        return ebSplitAccountDao
                .selectList(new LambdaQueryWrapper<EbSplitAccount>().eq(EbSplitAccount::getOrderNo, orderNo)
                );
    }

    @Override
    public void setEbSplitAccount(EbSplitAccount ebSplitAccount) {
        ebSplitAccount.setStatus(true);
        Date date = new Date();
        ebSplitAccountDao.updateById(ebSplitAccount);
        if (ebSplitAccount.getAgentMerId() != null) {
            EbMerchantAccount ebMerchantAccount =
                    ebMerchantAccountDao
                            .selectOne(new LambdaQueryWrapper<EbMerchantAccount>()
                                    .eq(EbMerchantAccount::getMerId, ebSplitAccount.getMerId()));
            BigDecimal amount = BigDecimal.ZERO;
            if (ebMerchantAccount == null) {
                //市代理账号
                ebMerchantAccount = new EbMerchantAccount();
                ebMerchantAccount.setMerId(ebSplitAccount.getAgentMerId());
                ebMerchantAccount.setMerName(ebSplitAccount.getAgentName());
                ebMerchantAccount.setBalance(BigDecimal.ZERO);
                ebMerchantAccount.setAvailableBalance(BigDecimal.ZERO);
                ebMerchantAccount.setFrozenBalance(BigDecimal.ZERO);
                ebMerchantAccount.setCreateTime(date);
                ebMerchantAccount.setUpdateTime(date);
                ebMerchantAccountDao.insert(ebMerchantAccount);
            } else {
                amount = ebMerchantAccount.getBalance();
                ebMerchantAccount.setBalance(ebSplitAccount.getAgentAmount().add(ebMerchantAccount.getBalance()));
                ebMerchantAccount.setFrozenBalance(ebSplitAccount.getAgentAmount().add(ebMerchantAccount.getFrozenBalance()));

                ebMerchantAccount.setUpdateTime(date);

                ebMerchantAccountDao.updateById(ebMerchantAccount);

            }
            EbCommissionTransaction ebCommissionTransaction = new EbCommissionTransaction();
            ebCommissionTransaction.setAccountId(String.valueOf(ebMerchantAccount.getId()));
            ebCommissionTransaction.setTransactionNo(CrmebUtil.getOrderNo(OrderConstants.ORDER_PREFIX_WECHAT));
            ebCommissionTransaction.setMerchantName(ebSplitAccount.getAgentName());
            ebCommissionTransaction.setAmount(ebSplitAccount.getAgentAmount());
            ebCommissionTransaction.setBeforeBalance(amount);
            ebCommissionTransaction.setAfterBalance(ebMerchantAccount.getBalance());
            ebCommissionTransaction.setTransactionTime(date);
            ebCommissionTransaction.setRelatedOrder(ebSplitAccount.getOrderNo());
            ebCommissionTransaction.setCreateTime(date);
            ebCommissionTransactionDao.insert(ebCommissionTransaction);
            EbWithdrawalApplication ebWithdrawalApplication = new EbWithdrawalApplication();
            ebWithdrawalApplication.setAccountId(String.valueOf(ebMerchantAccount.getId()));
            ebWithdrawalApplication.setApplicationNo(CrmebUtil.getOrderNo(OrderConstants.ORDER_PREFIX_WECHAT));
            ebWithdrawalApplication.setMerName(ebMerchantAccount.getMerName());
            ebWithdrawalApplication.setApplicantId(Long.valueOf(ebMerchantAccount.getMerId()));
            ebWithdrawalApplication.setWithdrawalAmount(ebSplitAccount.getAgentAmount());
            ebWithdrawalApplication.setServiceFee(BigDecimal.ZERO);
            ebWithdrawalApplication.setActualAmount(ebSplitAccount.getAgentAmount());
            ebWithdrawalApplication.setApplicationTime(date);
            ebWithdrawalApplication.setPaymentPlatform("微信");
            ebWithdrawalApplication.setPaymentAccount("第三方支付平台");
            ebWithdrawalApplication.setReceiverAccount("商户账户");
            ebWithdrawalApplication.setAuditStatus(0);
            ebWithdrawalApplication.setPaymentStatus(1);
            ebWithdrawalApplication.setCreateTime(date);
            ebWithdrawalApplication.setUpdateTime(date);
            ebWithdrawalApplication.setTransactionNo(ebSplitAccount.getOrderNo());
            ebWithdrawalApplicationDao.insert(ebWithdrawalApplication);
        }
        if (ebSplitAccount.getMerchandiserMerId() != null) {
            BigDecimal amount = BigDecimal.ZERO;

            //选品人账号
            EbMerchantAccount ebMerchantAccount =
                    ebMerchantAccountDao
                            .selectOne(new LambdaQueryWrapper<EbMerchantAccount>()
                                    .eq(EbMerchantAccount::getMerId, ebSplitAccount.getMerId()));
            if (ebMerchantAccount == null) {
                ebMerchantAccount = new EbMerchantAccount();
                ebMerchantAccount.setMerId(ebSplitAccount.getMerchandiserMerId());
                ebMerchantAccount.setMerName(ebSplitAccount.getMerchandiserName());
                ebMerchantAccount.setBalance(BigDecimal.ZERO);
                ebMerchantAccount.setAvailableBalance(BigDecimal.ZERO);
                ebMerchantAccount.setFrozenBalance(BigDecimal.ZERO);
                ebMerchantAccount.setCreateTime(date);
                ebMerchantAccount.setUpdateTime(date);
                ebMerchantAccountDao.insert(ebMerchantAccount);
            } else {
                amount = ebMerchantAccount.getBalance();
                ebMerchantAccount.setBalance(ebSplitAccount.getMerchandiserAmount().add(ebMerchantAccount.getBalance()));
                ebMerchantAccount.setFrozenBalance(ebSplitAccount.getMerchandiserAmount().add(ebMerchantAccount.getFrozenBalance()));
                ebMerchantAccount.setUpdateTime(date);
                ebMerchantAccountDao.updateById(ebMerchantAccount);

            }
            //增加流水记录
            EbCommissionTransaction ebCommissionTransaction = new EbCommissionTransaction();
            ebCommissionTransaction.setAccountId(String.valueOf(ebMerchantAccount.getId()));
            ebCommissionTransaction.setTransactionNo(CrmebUtil.getOrderNo(OrderConstants.ORDER_PREFIX_WECHAT));
            ebCommissionTransaction.setMerchantName(ebSplitAccount.getMerchandiserName());
            ebCommissionTransaction.setAmount(ebSplitAccount.getAgentAmount());
            ebCommissionTransaction.setBeforeBalance(amount);
            ebCommissionTransaction.setAfterBalance(ebMerchantAccount.getBalance());
            ebCommissionTransaction.setTransactionTime(date);
            ebCommissionTransaction.setRelatedOrder(ebSplitAccount.getOrderNo());
            ebCommissionTransaction.setCreateTime(date);
            ebCommissionTransactionDao.insert(ebCommissionTransaction);
            EbWithdrawalApplication ebWithdrawalApplication = new EbWithdrawalApplication();
            ebWithdrawalApplication.setAccountId(String.valueOf(ebMerchantAccount.getId()));
            ebWithdrawalApplication.setApplicationNo(CrmebUtil.getOrderNo(OrderConstants.ORDER_PREFIX_WECHAT));
            ebWithdrawalApplication.setMerName(ebCommissionTransaction.getMerchantName());
            ebWithdrawalApplication.setApplicantId(Long.valueOf(ebSplitAccount.getMerchandiserMerId()));
            ebWithdrawalApplication.setWithdrawalAmount(ebSplitAccount.getMerchandiserAmount());
            ebWithdrawalApplication.setServiceFee(BigDecimal.ZERO);
            ebWithdrawalApplication.setActualAmount(ebSplitAccount.getMerchandiserAmount());
            ebWithdrawalApplication.setApplicationTime(date);
            ebWithdrawalApplication.setPaymentPlatform("微信");
            ebWithdrawalApplication.setPaymentAccount("第三方支付平台");
            ebWithdrawalApplication.setReceiverAccount("商户账户");
            ebWithdrawalApplication.setAuditStatus(0);
            ebWithdrawalApplication.setPaymentStatus(1);
            ebWithdrawalApplication.setCreateTime(date);
            ebWithdrawalApplication.setUpdateTime(date);
            ebWithdrawalApplication.setTransactionNo(ebSplitAccount.getOrderNo());
            ebWithdrawalApplicationDao.insert(ebWithdrawalApplication);
        }
        ebSplitAccount.setId(null);
        ebSplitAccount.setOrderType("0");
        ebSplitAccountDao.insert(ebSplitAccount);
    }
}
